﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using teavch.Util.Formatter;
using teavch.Util.Formatter.SourceCode;

namespace teavch.Util.Formatter
{

	public partial class frmFormatter : Form
	{

		public frmFormatter()
		{
			InitializeComponent();
		}

		private void btnClose_Click( object sender, EventArgs e )
		{
			CancelEventArgs cea = new CancelEventArgs();
			cea.Cancel = false;
			Application.Exit( cea );
		}

		private void UpdateOutput( string html )
		{
			txtOutput.Text = html;
			wbHtmlView.Url = null;
			wbHtmlView.Refresh( WebBrowserRefreshOption.Normal );
			StringBuilder sb = this.getHtml( html );
			System.IO.File.WriteAllText( Application.StartupPath + "\\html\\sample.html", sb.ToString() );
			System.Threading.Thread.Sleep( 1000 );
			wbHtmlView.Url = new Uri( Application.StartupPath + "\\html\\sample.html" );
			wbHtmlView.Refresh( WebBrowserRefreshOption.Normal );
		}

		private StringBuilder getHtml( string html )
		{
			StringBuilder sb = new System.Text.StringBuilder( "<!DOCTYPE html>" + System.Environment.NewLine +
								"<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">" + System.Environment.NewLine +
								"<head><meta charset=\"utf-8\" />" + System.Environment.NewLine +
								"<title></title>" + System.Environment.NewLine +
								"<link href=\"" + Application.StartupPath + "\\css\\SqlCode.css" + "\" rel=\"stylesheet\" type=\"text/css\" />" + System.Environment.NewLine +
								"<link href=\"" + Application.StartupPath + "\\css\\CssCode.css" + "\" rel=\"stylesheet\" type=\"text/css\" />" + System.Environment.NewLine +
								"<link href=\"" + Application.StartupPath + "\\css\\CSharpCode.css" + "\" rel=\"stylesheet\" type=\"text/css\" />" + System.Environment.NewLine +
								"<link href=\"" + Application.StartupPath + "\\css\\CCode.css" + "\" rel=\"stylesheet\" type=\"text/css\" />" + System.Environment.NewLine +
								"<link href=\"" + Application.StartupPath + "\\css\\VBCode.css" + "\" rel=\"stylesheet\" type=\"text/css\" />" + System.Environment.NewLine +
								"<link href=\"" + Application.StartupPath + "\\css\\HtmlCode.css" + "\" rel=\"stylesheet\" type=\"text/css\" />" + System.Environment.NewLine +
								"</head>" + System.Environment.NewLine +
								"<body>" + System.Environment.NewLine +
								html +
								"</body>" + System.Environment.NewLine +
								"</html>" );
			return sb;
		}

		private void CallFormatting( object sender, EventArgs e )
		{
			Button btn = sender as Button;
			if ( btn != null )
			{
				this.UpdateOutput( txtInput.Text.ToString( btn.Tag.ToString(), false, true ) );
			}
		}
	}

}
